package com.bc.trade.controller;

import com.bc.common.CommonEnum.Resp;
import com.bc.common.message.AccountBalanceChangeReq;
import com.bc.common.message.AccountTransferReq;
import com.bc.common.message.BaseResp;
import com.bc.trade.config.R;
import com.bc.trade.config.SystemConfig;
import com.bc.trade.model.Org;
import com.bc.trade.model.TradeStrategy;
import com.bc.trade.service.OrderService;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSJson;
import com.fs.common.util.FSLog;
import com.fs.common.util.FSMD5;
import com.jfinal.aop.Enhancer;

/**
 * 清算收入控制器
 * 
 * @author Reed
 * @date 2018年7月2日
 */
public class IncomeController extends FSController {
	private static final Integer ZERO_FEE_ID = SystemConfig.config.getInt("zero_fee_id");
	private OrderService orderService = Enhancer.enhance(OrderService.class);

	public void charge() {
		String reqString = getStringFromRequest();
		FSLog.api.info("接收到清算收入请求数据 ------ > " + reqString);
		AccountBalanceChangeReq req = FSJson.toObject(reqString, AccountBalanceChangeReq.class);
		BaseResp resp = new BaseResp(req);
		String safeKey = Org.dao.selectSafeKey(req.getOrgCode());
		String sign = FSMD5.sortSign(req, safeKey);
		if(!sign.equals(req.getSign())){
			resp.setCodeAndMsg(Resp.Fail.getCode(), R.MSG_SIGN_ERROR);
			renderJson(resp);
			return;
		}
		
		TradeStrategy ts = new TradeStrategy();
		ts.setMerchFeeId(ZERO_FEE_ID);
		ts.setChannelCode(req.getChannelNo());
		resp = orderService.charge(req, ts);
		renderJson(resp);
	}

	public void transfer() {
		String reqString = getStringFromRequest();
		FSLog.api.info("接收到机构分润请求数据 ------ > " + reqString);
		AccountTransferReq req = FSJson.toObject(reqString, AccountTransferReq.class);
		BaseResp resp = new BaseResp(req);
		String safeKey = Org.dao.selectSafeKey(req.getOrgCode());
		String sign = FSMD5.sortSign(req, safeKey);
		if(!sign.equals(req.getSign())){
			resp.setCodeAndMsg(Resp.Fail.getCode(), R.MSG_SIGN_ERROR);
			renderJson(resp);
			return;
		}
		
		TradeStrategy ts = new TradeStrategy();
		ts.setChannelCode(req.getChannelNo());
		ts.setMerchFeeId(ZERO_FEE_ID);
		resp = orderService.transfer(req, ts);
		renderJson(resp);
	}

}
